<template>
  <div>
    <table-box ref="tableBox" no-request show-index show-selection :other-filters="comFiltersRaw" :other-raw="paramsRaw" :table-config="tableConfig" @selection-change="handleSelectionChange" @btnOperate="handleBtnOperate" />
    <export-files
      v-if="exportisible"
      title="导出"
      :visible.sync="exportisible"
      download-url=""
      download-prefix=""
      :config="exportConfig"
      :success-fun="(config,res)=>{}"
      @success="handleExportSuccess"
    />
    <import-files
      title="导入"
      :visible.sync="importVisible"
      :config="importConfig"
      :success-fun="(config,res)=>{}"
      @success="handleImportSuccess"
      @close="handleImportClose"
    />
    <Reallocate ref="reallocate" :rows="selectedRows" @on-success="refreshTableBoxData" />
  </div>
</template>
<script>
import Reallocate from './Reallocate'
import { getToken } from '@/utils/auth'
// import { downImportTemplate } from '@/api/channelTask2/potentialCustomer'
export default {
  name: 'Table',
  components: { Reallocate },
  props: {
    filtersRaw: {
      type: Array,
      default: () => []
    },
    paramsRaw: {
      type: Object,
      default: () => ({})
    },
    type: {
      type: String,
      default: 'all'
    }
  },
  data() {
    return {
      tableConfig: {
        interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/ctmIntendedCustomer/page`,
        hideSearchBar: true,
        buttonGroupCode: 'all',
        tabIndex: 1,
        sort: 'createTime',
        order: 'DESC',
        setting: [
          {
            prop: 'customerPhone',
            label: '意向客户电话',
            className: 'btn-green',
            minWidth: 130,
            isButton: true,
            clickFun: this.toDetail
          },
          {
            prop: 'customerName',
            label: '意向客户姓名',
            minWidth: 130
          },
          {
            prop: 'channel',
            label: '来源渠道',
            minWidth: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'CTM_POTENTIAL_CUSTOMER_SOURCE'
            }
          },
          {
            prop: 'isHaveShop',
            label: '是否有店面',
            minWidth: 130,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'BOOLEANS'
            }
          },
          {
            prop: 'agencyName',
            label: '所属办事处',
            minWidth: 130
          },
          {
            prop: 'intendedCity',
            label: '意向加盟城市',
            minWidth: 130
          },
          {
            prop: 'followUpStage',
            label: '跟进进度',
            minWidth: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'CTM_INTEND_CUS_FOLLOW_UP_PROGRESS'
            }
          },
          {
            prop: 'managerName',
            label: '招商经理',
            minWidth: 120
          },
          {
            prop: 'intendedLevel',
            label: '意向等级',
            minWidth: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'CTM_INTENTION_LEVEL'
            }
          },
          {
            prop: 'intendedBusiness',
            label: '意向经营业务类型',
            minWidth: 160,
            render: (h, row) => {
              return h('c-mdm-view', {
                props: {
                  value: row.intendedBusiness,
                  label: row.intendedBusinessName,
                  docCode: 'QY037'
                }
              })
            }
          },
          {
            prop: 'customerSource',
            label: '客户来源',
            minWidth: 120,
            propEnum: {
              appCode: 'slm',
              domainCode: 'marketing',
              dictTypeCode: 'CTM_TOBE_CUSTOMER_DATA_FROM'
            }
          }
        ],
        btnList: process.env.NODE_ENV === 'development' ? [
          { buttonName: '新增', optCode: 'add', outerFlag: true },
          { buttonName: '编辑', optCode: 'edit', outerFlag: true },
          { buttonName: '重新分配', optCode: 'reallocate', outerFlag: true },
          { buttonName: '分配', optCode: 'allocate', outerFlag: true },
          { buttonName: '导出', optCode: 'export', outerFlag: true },
          { buttonName: '导入', optCode: 'import', outerFlag: true }
          // { buttonName: '下载导入模板', optCode: 'down_import_temp', outerFlag: true }
        ] : null
      },
      exportisible: false,
      selectedRows: [],
      importVisible: false
    }
  },
  computed: {
    exportConfig() {
      return [
        {
          code: '2',
          name: '意向客户管理模板',
          async: false, // 同步导出
          templateCode: 'ctmIntendedCustomerExcelDTO', // 模板code
          templateApi: `${process.env.VUE_APP_BASE_API}/api/v1/ctmIntendedCustomer/export` // 模板导出api
        },
        {
          code: '1',
          name: '意向客户管理',
          async: false, // 同步导出数据
          templateCode: '', // 模板code
          dataApi: `${process.env.VUE_APP_BASE_API}/api/v1/ctmIntendedCustomer/export`, // 数据导出api
          headers: { token: getToken() },
          filtersRaw: this.comFiltersRaw, // 符合当前查询条件的所有数据行的导出条件(当前数据查询结果对应的条件)
          paramsRaw: this.paramsRaw,
          sort: 'createTime', // 排序字段
          order: 'desc', // 排序方式
          selectedRows: this.selectedRows, // 当前所选中的数据行
          queryField: 'id'// 指定行导出时使用的查询字段名称
        }
      ]
    },
    comFiltersRaw() {
      const t = [...this.filtersRaw]
      if (this.type === 'tobe_allocate') {
        t.push({ id: 'investmentManagerId', property: 'investmentManagerId', value: '', operator: 'IS NULL' })
      }
      return t
    },
    importConfig() {
      return [
        {
          code: '1',
          name: '意向客户管理新增导入',
          async: false,
          headers: { token: getToken() },
          apiUrl: `${process.env.VUE_APP_BASE_API}/api/v1/ctmIntendedCustomer/importExcelData`
        },
        {
          code: '2',
          name: '意向客户管理编辑导入',
          async: false,
          headers: { token: getToken() },
          apiUrl: `${process.env.VUE_APP_BASE_API}/api/v1/ctmIntendedCustomer/editImportExcelData`
        }
      ]
    }
  },
  created() {
    this.$set(this.tableConfig, 'buttonGroupCode', this.type)
    if (this.type === 'tobe_allocate') {
      this.$set(this.tableConfig, 'tableIndex', 2)
      for (let i = 0; i < this.tableConfig.setting.length; i++) {
        if (this.tableConfig.setting[i].prop === 'intendedCity') {
          this.tableConfig.setting.splice(i, 1)
          break
        }
      }
    }
  },
  mounted() {
    this.refreshTableBoxData()
  },
  methods: {
    refreshTableBoxData(flag) {
      this.$refs.tableBox && this.$refs.tableBox.initTableData(flag)
    },
    refreshTableHeight() {
      this.$refs.tableBox && this.$refs.tableBox.refreshTableHeight()
    },
    handleSelectionChange(rows) {
      this.selectedRows = rows
    },
    handleBtnOperate(flag) {
      this[flag] && this[flag]()
    },
    add() {
      this.$pageStack.push('src/views/channelTask2/attractInvestment/potentialCustomer/components/AddEdit.vue', {
        props: {}
      }, (flag) => {
        flag && this.refreshTableBoxData(flag)
      })
    },
    edit() {
      if (!this.singleJudge(this.selectedRows, '编辑', true)) {
        return
      }
      const d = this.selectedRows[0]
      if (d.followUpStage !== 'A') {
        this.$message.warning('只能编辑跟进进度为【待分配】的客户数据!')
        return
      }
      this.$pageStack.push('src/views/channelTask2/attractInvestment/potentialCustomer/components/AddEdit.vue', {
        props: {
          id: d.id
        }
      }, (flag) => {
        flag && this.refreshTableBoxData(flag)
      })
    },
    toDetail(row) {
      this.$pageStack.push('src/views/channelTask2/attractInvestment/potentialCustomer/components/detail.vue', {
        props: {
          id: row.id
        }
      })
    },
    export() {
      this.exportisible = true
    },
    import() {
      this.importVisible = true
    },
    // down_import_temp() {
    //   downImportTemplate('意向客户管理导入模板.xlsx').then(() => {
    //     this.$message.success('模板下载成功!')
    //   })
    // },
    reallocate() {
      if (!this.singleJudge(this.selectedRows, '重新分配', true)) {
        return
      }
      if (!this.selectedRows[0].investmentManagerId) {
        this.$message.warning('所选项未分配招商经理')
        return
      }
      if (this.selectedRows[0].followUpStage !== 'B') {
        this.$message.warning('只能重新分配跟进进度为【洽谈中】的客户数据!')
        return
      }
      this.$refs.reallocate.openDialog(this.selectedRows[0], '重新分配')
    },
    allocate() {
      if (!this.singleJudge(this.selectedRows, '分配', true)) {
        return
      }
      if (this.selectedRows[0].investmentManagerId) {
        this.$message.warning('所选项已分配招商经理')
        return
      }
      this.$refs.reallocate.openDialog(this.selectedRows[0], '分配')
    },
    handleExportSuccess() {

    },
    handleImportSuccess() {
      this.refreshTableBoxData()
    },
    handleImportClose() {

    }
  }
}
</script>
